package com.example.okhttpstudy;

import androidx.appcompat.app.AppCompatActivity;

import okhttp3.ResponseBody;
import retrofit2.*;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.okhttpstudy.netinterface.HttpService;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG=MainActivity.class.getSimpleName();

    private OkHttpClient httpClient;
    private Retrofit retrofit;
    private HttpService httpService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        httpClient=new OkHttpClient();

        retrofit = new Retrofit.Builder().baseUrl("https://www.httpbin.org")
                .build();
        httpService = retrofit.create(HttpService.class);


    }

    public void getSync(View view) {

        new Thread(()->{
            Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build();
            //准备好请求的call对象
            Call call = httpClient.newCall(request);
            try {
                Response response = call.execute();
                Log.i(TAG,"getSync"+response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();


    }

    public void getAsync(View view) {

        Request request = new Request.Builder().url("https://www.httpbin.org/get?a=1&b=2").build();
        //准备好请求的call对象
        Call call = httpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                if (response.isSuccessful()){
                    Log.i(TAG,"getAsync"+response.body().string());
                }
            }
        });

        Log.i(TAG,"异步。。。。。");
    }

    public void postSync(View view) {

        new Thread(()->{
            FormBody formBody = new FormBody.Builder().add("a", "kele").add("b", "pepsi").build();
            Request request = new Request.Builder().url("https://www.httpbin.org/post")
                    .post(formBody).build();
            //准备好请求的call对象
            Call call = httpClient.newCall(request);
            try {
                Response response = call.execute();
                Log.i(TAG,"postSync"+response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    public void postAsync(View view) {
        //okhttp方式
//        FormBody formBody = new FormBody.Builder().add("a", "kele").add("b", "pepsi").build();
//        Request request = new Request.Builder().url("https://www.httpbin.org/post").post(formBody).build();
//        //准备好请求的call对象
//        Call call = httpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//            }
//            @Override
//            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//
//                if (response.isSuccessful()){
//                    Log.i(TAG,"postAsync"+response.body().string());
//                }
//            }
//        });
//
//        Log.i(TAG,"异步。。。。。");

        //retrofit方式
        retrofit2.Call<ResponseBody> call = httpService.post("kele", "123");
        call.enqueue(new retrofit2.Callback<ResponseBody>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                try {
                    Log.i(TAG,"postAsync"+response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {

            }
        });

    }
}